#include "stdafx.h"
#include "GameStateMgr.h"
#include "WeakRescurtState.h"
#include "Factory.h"
#include "GameAnalyze.h"

CWeakRescurtState::CWeakRescurtState()
{
    Name = _T("CWeakRescurtState");
}

void CWeakRescurtState::Reset()
{
    CPlayer& Player = *IFactory::getSingleton().GetPlayer();
    Player.EndMoving();
}

void CWeakRescurtState::DoAction()
{
    CPlayer& Player = *IFactory::getSingleton().GetPlayer();

    if (Player.Helath()==0)
    {
        DWORD dw = 5000;
        if (GET_DB().m_dwRescurtType==1)
            dw = 500;
        if (GetTickCount()-dwTime>dw)
        {
            CWowFun::ExecuteScript("/script RepopMe()");
            dwTime = GetTickCount();
        }
        sMyTime::Instance().Sleep(dw);
        if (GET_DB().m_dwPatrolType==ENUM_FB_PATROL || GET_DB().m_dwPatrolType==ENUM_STAND_PATROL ||
            GET_DB().m_dwPatrolType==ENUM_FB_WAIT)
        {
            CGameAnalyze::getSingleton().SetWhere(E_CITY);
        }
        Player.EndMoving();
        return;
    }

    if (GET_DB().m_bBattle)
    {
        return;
    }

    if (Player.HasDebuf(0x2086))
    {
        sPos P = Player.GetPos();
        sPos T = Player.GetCorPose();

        CTarget* pHealer = NULL;
        DWORD dw = UNIT_NPC_FLAG_SPIRITHEALER;
        pHealer = IFactory::getSingleton().GetNPC(dw);
        if (!pHealer || pHealer->Distance()>100)
        {
            sMyTime::Instance().Sleep(5000);
            return;
        }

        if (pHealer->Distance() >5 )
        {
            DWORD dz = Player.MoveToPos(pHealer->GetPos(), E_FORWARD|E_ARRIVE_STOP|E_HARD_BLOCK, 5.0f);
            return;
        }

        Player.EndMoving();
        if (Player.Target()==pHealer->GetGuid())
        {
            CWowFun::ExecuteScript("/run AcceptXPLoss()");
            sMyTime::Instance().Sleep(3000);
        }
        else
        {
            CWowFun::RClick(pHealer->GetGuid());
            sMyTime::Instance().Sleep(2000);
        }
    }

}